<template>
  <el-row class="home" :gutter="20">
    <el-col :span="8">
      <el-card shadow="hover" style=" border: 1px solid red">
        <div class="user">
          <img :src="userImg" alt="" />
          <div class="userinfo">
            <p class="name">SAUL</p>
            <p class="access">超级管理员</p>
          </div>
        </div>
        <div class="login-info">
          <p>上次登录时间：<span>2020-03-28</span></p>
          <p>上次登录地点：<span>苏州</span></p>
        </div>
      </el-card>

      <el-card
        shadow="hover"
        style="height: 520px; margin-top: 20px; border: 1px solid red"
      >
        <el-table :data="tableData">
          <el-table-column
            show-overflow-tooltip=""
            v-for="(val, key) in tableLabel"
            :key="key"
            :label="val"
            :prop="key"
          >
          </el-table-column>
        </el-table>
      </el-card>
    </el-col>
    <el-col :span="16">
      <div class="num">
        <el-card
          shadow="hover"
          v-for="item in constData"
          :key="item.name"
          :body-style="{ display: 'flex', padding: '0' }"
        >
          <i
            class="icon"
            :style="{ background: item.color }"
            :class="`el-icon-${item.icon}`"
          ></i>
          <div class="detail">
            <div class="num">￥{{ item.value }}</div>
            <div class="text">{{ item.name }}</div>
          </div>
        </el-card>
      </div>

      <el-card shadow="hover">
        <echart
          :chartData="echartData.order"
          style="height: 280px; border: 1px solid red"
        ></echart>
      </el-card>

      <div class="graph">
        <el-card shadow="hover">
          <echart
            :chartData="echartData.user"
            style="height: 260px; border: 1px solid red"
          ></echart>
        </el-card>
        <el-card shadow="hover">
          <echart
            :chartData="echartData.video"
            style="height: 260px; border: 1px solid red"
            :isAxisChart="false"
          ></echart>
        </el-card>
      </div>
    </el-col>
  </el-row>
</template>
<script>
import Echart from "../../components/EChart";
export default {
  name: "Home",
  components: {
    Echart
  },
  mounted() {},
  data() {
    return {
      userImg: require("../../assets/images/user.png"),
      constData: [
        {
          name: "今日支付订单",
          value: "1234",
          icon: "s-release",
          color: "#00BCF2"
        },
        {
          name: "今日收藏订单",
          value: "652",
          icon: "takeaway-box",
          color: "#42B883"
        },
        {
          name: "今日购物订单",
          value: "8541",
          icon: "s-platform",
          color: "#395D97"
        },
        {
          name: "今日返回订单",
          value: "624",
          icon: "s-marketing",
          color: "#D7D444"
        },
        {
          name: "今日评价订单",
          value: "965",
          icon: "s-shop",
          color: "#202020"
        },
        {
          name: "今日分享订单",
          value: "85",
          icon: "share",
          color: "#344B5E"
        }
      ],
      tableData: [],
      tableLabel: {
        name: "课程",
        todayBuy: "今日购买",
        monthBuy: "本月购买",
        totalBuy: "总购买"
      },
      echartData: {
        order: {
          xData: [],
          series: []
        },
        user: {
          xData: [],
          series: []
        },
        video: {
          series: []
        }
      }
    };
  },
  methods: {
    getTableData() {
      this.$http.get("/home/getData").then(res => {
        res = res.data;
        this.tableData = res.data.tableData;

        // 订单折线图
        const order = res.data.orderData;
        this.echartData.order.xData = order.date;
        // 获取 series 中的 name 部分一键名
        let keyArray = Object.keys(order.data[0]);

        // 折现图
        keyArray.forEach(key => {
          this.echartData.order.series.push({
            name: key === "wechat" ? "小程序" : key,
            data: order.data.map(item => item[key]),
            type: "line"
          });
        });

        // 用户
        this.echartData.user.xData = res.data.userData.map(item => item.date);
        this.echartData.user.series.push({
          name: "新增用户",
          data: res.data.userData.map(item => item.new),
          type: "bar"
        });

        this.echartData.user.series.push({
          name: "活跃用户",
          data: res.data.userData.map(item => item.active),
          type: "bar",
          barGap: 0
        });

        // video
        this.echartData.video.series.push({
          data: res.data.videoData,
          type: "pie"
        });
      });
    }
  },
  created() {
    this.getTableData();
  }
};
</script>
<style lang="scss" scoped>
@import "~@/assets/scss/home";
</style>
